﻿using Coldairarrow.Business.PB;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.Util;
using Coldairarrow.Util.ClassLibrary;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using NPOI.POIFS.Crypt.Dsig;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.PB
{
    [Route("/PB/[controller]/[action]")]
    public class PB_StockUpController : BaseApiController
    {
        #region DI
        private readonly MongoDBOptions _mongodbOptions;
        public PB_StockUpController(IPB_StockUpBusiness pB_StockUpBus, IOptions<MongoDBOptions> mongodbOptions)
        {
            _pB_StockUpBus = pB_StockUpBus;
            _mongodbOptions = mongodbOptions.Value;
        }

        IPB_StockUpBusiness _pB_StockUpBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<PB_PickUpList>> GetDataList(PageInput<ConditionDTO> input)
        {
            return await _pB_StockUpBus.GetDataListAsync(input);
        }

        [HttpPost]
        public async Task<PB_PickUpList> GetTheData(IdInputDTO input)
        {
            return await _pB_StockUpBus.GetTheDataAsync(input.id);
        }

        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(PB_PickUpList data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                await _pB_StockUpBus.AddDataAsync(data);
            }
            else
            {
                await _pB_StockUpBus.UpdateDataAsync(data);
            }
        }

        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _pB_StockUpBus.DeleteDataAsync(ids);
        }

        #endregion

        /// <summary>
        /// 发货通知单下的保存备货通知单
        /// </summary>
        /// <param name="data">保存设计清单及明细</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AjaxResult> SaveMaterialData(SaveStockData data)
        {
            if (data.PData.Id.IsNullOrEmpty())
            {
                InitEntity(data);

               return  await _pB_StockUpBus.AddMaterialData(data);
            }
            else
            {
               return await _pB_StockUpBus.UpdateMaterialData(data);
            }
        }
        /// <summary>
        /// 分批次备货保存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AjaxResult> AddStepsPickMain(AddStepsPickMain data) {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                return await _pB_StockUpBus.AddStepsPickMain(data);
            }
            else {
                return await _pB_StockUpBus.UpdateStepsPickMain(data);
            }
        }
        /// <summary>
        /// 获取备货单对应的包明细
        /// </summary>
        /// <param name="stockupQrCode">备货单二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_UnLoadListDataDTO>> GetStockUpDetailListData(string stockupQrCode)
        {
            return await _pB_StockUpBus.GetStockUpDetailListDataAsync(stockupQrCode);
        }

        /// <summary>
        /// 删除备货单已出库的明细
        /// </summary>
        /// <param name="stockupQrCode">备货单二维码</param>
        /// <param name="packqrocde">包二维码</param>
        /// <param name="Nums">包二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeleteStockUpDetailListDataAsync(string stockupQrCode, string packqrocde, double Nums)
        {
            await _pB_StockUpBus.DeleteStockUpDetailListDataAsync(stockupQrCode, packqrocde, Nums);
        }

        /// <summary>
        /// 出库环节,获取包的信息
        /// </summary>
        /// <param name="PackQrcode">包二维码</param>
        /// <param name="stockupQrCode">备货单二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<StockUpPackDTO> GetStockUpPackData(string PackQrcode, string stockupQrCode)
        {
            return await _pB_StockUpBus.GetStockUpPackDataAsync(PackQrcode, stockupQrCode);
        }

        /// <summary>
        /// 出库环节,获取单个成品物料信息
        /// </summary>
        /// <param name="JobQrCode">单个成品二维码</param>
        /// <param name="LocaQrcode">货位二维码</param>
        /// <param name="stockupQrCode">备货单二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<StockMaterialDTO> GetStockMaterialData(string JobQrCode, string LocaQrcode, string stockupQrCode)
        {
            return await _pB_StockUpBus.GetStockMaterialDataAsync(JobQrCode, LocaQrcode, stockupQrCode);
        }

        /// <summary>
        /// 扫描包出库,并对备货单做对应物料数量做修改
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpdateStockUpData(OutStockData data)
        {
            await _pB_StockUpBus.UpdateStockUpDataAsync(data);
        }

        /// <summary>
        /// 获取设计清单步级
        /// </summary>
        /// <param name="stockupId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int?> GetStepsIndex(string ParentId) {
            return await _pB_StockUpBus.GetStepsIndex(ParentId);
        }
        /// <summary>
        /// 下架出库复核
        /// </summary>
        /// <param name="stockupId">备货单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task SubmitStockUpData(string stockupId)
        {
            await _pB_StockUpBus.SubmitStockUpDataAsync(stockupId);
        }

        /// <summary>
        /// 下架出库提交
        /// </summary>
        /// <param name="stockupQrCode">备货单二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task SubmitStockUpToReview(string stockupQrCode)
        {
            await _pB_StockUpBus.SubmitStockUpToReviewAsync(stockupQrCode);
        }

        /// <summary>
        /// 备货单提交返回未备物料信息
        /// </summary>
        /// <param name="stockupQrCode">备货单二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<StockUpMaterialDTO>> GetStockUpMaterialList(string stockupQrCode)
        {
            return await _pB_StockUpBus.GetStockUpMaterialListAsync(stockupQrCode);
        }

        /// <summary>
        /// 判断下架出库的包对应货位的项目是否与备货单对应的项目一致
        /// </summary>
        /// <param name="packqrcode">包二维码</param>PB_CirculationBill
        /// <param name="stockupQrCode">备货单二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> GetLocationProjectTruOrFalse(string packqrcode, string stockupQrCode)
        {
            return await _pB_StockUpBus.GetLocationProjectTruOrFalseAsync(packqrcode, stockupQrCode);
        }

        /// <summary>
        /// 出库扫描单个物料
        /// </summary>
        /// <param name="packqrcode">物料二维码</param>
        /// <param name="stockupQrCode">备货单二维码</param>
        /// <param name="locaQrcode">货位二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_PickUpListDataDTO> GetMaterialData(string packqrcode,string stockupQrCode, string locaQrcode)
        {
            return await _pB_StockUpBus.GetMaterialDataAsync(packqrcode, stockupQrCode, locaQrcode);
        }


        /// <summary>
        /// 取消备货单提交
        /// </summary>
        /// <param name="stockupQrCode">备货单二维码</param>
        /// 增加取消备货单提交功能   2022-02-20   赵锦涛
        /// <returns></returns>
        [HttpPost]
        public async Task CancelStockUpAsync(string stockupQrCode)
        {
            await _pB_StockUpBus.CancelStockUpAsync(stockupQrCode);
        }

        /// <summary>
        /// 初始化出库单表数据
        /// 只作用2022-03更新生产库使用
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task ResetStockUpDataAsync()
        {
            await _pB_StockUpBus.ResetStockUpDataAsync();
        }
        /// <summary>
        /// 根据发货通知单明细 获取发货计划 明细
        /// </summary>
        /// <param name="input"></param>
        /// <param name="ReserveParent">关联的发货通知单明细id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<PB_DeliveryPlanDetailsDTO>> GetDeliveryPlanDetails(PageInput<ConditionDTO> input, string ReserveParent)
        {
            var PageData = await _pB_StockUpBus.GetDeliveryPlanDetails(input, ReserveParent);
            return PageData;
        }

        /// <summary>
        /// 对7月之前未扫码的备货单做数据处理,生成出库单,更改库存占用量,更改备货单的状态,在备货单Remarks字段增加备货单的备注
        /// </summary>
        /// <param name="PickMainId">备货单ID</param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpdateInventoryAsync(string PickMainId)
        {
            await _pB_StockUpBus.UpdateInventoryAsync(PickMainId);
        }
        [HttpPost]
        public async Task<AddStepsPickMain> GetStepPickMain(string ParentId,string ItemId) {
            return await _pB_StockUpBus.GetStepPickMain(ParentId, ItemId);
        }
        [HttpPost]
        public async Task<List<StepsSummary>> GetStepsSummaryList(string ParentId) {
            var res = await _pB_StockUpBus.GetStepsSummaryList(ParentId);
            return res;
        }

        [HttpPost]
        public async Task preparationSteps(PreparationStepsSearch data){
            await _pB_StockUpBus.preparationSteps(data, _mongodbOptions);
        }


        /// <summary>
        /// 修改备货单为完成状态
        /// </summary>
        /// <param name="PickMainId">备货单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task SubmitPickMainDataAsync(string PickMainId)
        {
            await _pB_StockUpBus.SubmitPickMainDataAsync(PickMainId);
        }

    }
}